#include<bits/stdc++.h>
using namespace std;
#define ll long long
int read(){
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x;
}

int n,m,a[100005],c[100005],x[4],lg,T,b[100005],t[200005],st[100005],e[131075<<1],dt,pre[100],L[131075<<1],R[131075<<1];
ll ans,all;
bool vis[100005],sub[100005];
bool p[105][105];
char s[105];
ll asfd;
ll calc(ll l,ll r){
    return (r+l)*(r-l+1)>>1;
}
int h(int a,int r){
    return pre[r-1]+(a-1>>r-1)+1;
}
void dfs(int a,int r,int c,int idx){
    if(idx==1)return;
    e[idx]=a;
    //cout<<a<<" "<<r<<" "<<c<<" "<<idx<<endl;
    if(idx&1){
        if(b[a]>=r||!p[r][c]){
            dfs(a,r+1,(c-1)/2+1,idx>>1);
            if(p[r][c])dt-=e[idx^1];
        }
        else
            dfs(e[idx^1],r+1,(c-1)/2+1,idx>>1),dt-=a;
    }
    else{
        if(p[r][c])return;
        if(b[a]>=r){

            sub[L[idx^1]]=1;
            if(R[idx^1]<=asfd){
                dt-=calc(L[idx^1],R[idx^1]);
            }
            dfs(a,r+1,(c-1)/2+1,idx>>1);
        }
        else dt-=a;
    }
}
void solve(){
    x[0]=read(),x[1]=read(),x[2]=read(),x[3]=read();
    for(int i=1;i<=n;i++)b[i]=a[i]^x[i%4],vis[i]=0,st[i]=1;
    //for(int i=1;i<=n;i++)cout<<b[i]<<" ";
    //puts("");
    for(int i=1;i<=1<<lg+1;i++)e[i]=0,sub[i]=0;
    dt=1,all=0;
    for(int i=1,j=0;i<=n;i++){
        if((1<<j)+1==i){
            j++;
            asfd=1<<j;
            if(!sub[i])dt+=calc(i,1<<j);
        }
        if(!vis[i])dfs(i,1,(i-1)/2+1,(1<<lg)+i-1);
        t[i]=dt;
    }
    //for(int i=1;i<=n;i++)cout<<t[i]<<" ";
   // puts("");
    for(int i=1;i<=m;i++)all^=(i*t[c[i]]);
    printf("%lld\n",all);
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=m;i++)c[i]=read();
    for(ll i=0;i<=60;i++)if((1LL<<i)>=n){lg=i;break;}
    for(int i=1;i<=lg;i++){
        scanf("%s",s+1);
        for(int j=1;j<=(1LL<<lg-i);j++)p[i][j]=s[j]-'0';
    }
    for(int i=(1<<lg+1)-1;i;i--){
        if(i>=1<<lg)L[i]=R[i]=i-(1<<lg)+1;
        else L[i]=L[i<<1],R[i]=R[i<<1|1];
    }
    T=read();
    while(T--)solve();
    return 0;
}
